/**
 * JBoot后台框架 v1.0.0
 * Copyright (c) 2017, Chen Peng All Rights Reserved.
 * <p>
 * 更改历史：
 * 2017-05-25 11:57    吴昭毅    创建
 */

package com.jdkreader.lambada;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 函数式编程示例
 *
 * @author 吴昭毅
 * @version 2017-05-22 16:58
 */
public class LambadaCode {


    public static void main(String[] args) {


        //Java8 以前的list排序方式
        List<String> names = Arrays.asList("peter","jack","mike");

        Collections.sort(names, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }
        });

        //函数式
        Collections.sort(names,(String a,String b) -> b.compareTo(a));


        String[] atp = {"Rafael Nadal", "Novak Djokovic",
                "Stanislas Wawrinka",
                "David Ferrer", "Roger Federer",
                "Andy Murray", "Tomas Berdych",
                "Juan Martin Del Potro"};
        List<String> players = Arrays.asList(atp);

       // 以前的循环方式

        for (String player : players) {
            System.out.print(player + "; ");
        }


        // 使用 lambda 表达式以及函数操作(functional operation)
        players.forEach((player) -> System.out.print(player + "; "));

        // 在 Java 8 中使用双冒号操作符(double colon operator)
        players.forEach(System.out::println);


        //使用匿名类
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello world!");
            }
        });
        //使用 lambda expression
        new Thread(() -> System.out.println("Hello world!")).start();

//        //根据name排序数组
//        String[] players = {"Rafael Nadal", "Novak Djokovic",
//                "Stanislas Wawrinka", "David Ferrer",
//                "Roger Federer", "Andy Murray",
//                "Tomas Berdych", "Juan Martin Del Potro",
//                "Richard Gasquet", "John Isner"};
//
//        //Arrays排序数组
//        Arrays.sort(players, new Comparator<String>() {
//            @Override
//            public int compare(String s1, String s2) {
//
//                return s1.compareTo(s2);
//            }
//        });
//        //lambada排列数组
//        Arrays.sort(players, (String s1, String s2) -> (s1.compareTo(s2)));
//        //输出字符串的Ascii码
//        System.out.println((int) 'a');


    }


}

